package com.zy.imagineCommunity.service;

import com.zy.imagineCommunity.dao.BlogMapper;
import com.zy.imagineCommunity.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BlogService {

    @Autowired
    BlogMapper blogMapper;

    public void addBlog(Blog blog) {
        blogMapper.addBlog(blog);
    }

    //根据类型、起始条数、每页个数查询文章
    public Page<BlogText> queryBlogByTypeAndPageNoAndPageSize(int type, int pageNo, int pageSize) {
        Page<BlogText> page = new Page<BlogText>();
        //设置当前页码
        page.setPageNo(pageNo);
        //设置每页个数
        page.setPageSize(pageSize);
        //设置总记录数
        int pageCountTotal = blogMapper.queryBlogCountTotalByType(type);
        page.setPageCountTotal(pageCountTotal);
        //设置总页码
        int pageTotal = pageCountTotal / pageSize;
        if (pageCountTotal % pageSize > 0) {
            pageTotal++;
        }
        page.setPageTotal(pageTotal);
        //计算起始条数，查询内容
        int begin = (pageNo - 1) * pageSize;
        List<BlogText> blogList = blogMapper.queryBlogByTypeAndBeginAndPageSize(type, begin, pageSize);
        //设置每页内容
        page.setItems(blogList);
        return page;
    }

    //根据id查询文章
    public BlogText queryBlogById(int id) {
        return blogMapper.queryBlogById(id);
    }

    //根据起始条数、每页个数查询文章
    public Page<BlogTextTwo> queryBlogPageNoAndPageSize(int pageNo, int pageSize) {
        Page<BlogTextTwo> page = new Page<BlogTextTwo>();
        //设置当前页码
        page.setPageNo(pageNo);
        //设置每页个数
        page.setPageSize(pageSize);
        //设置总记录数
        int pageCountTotal = blogMapper.queryBlogCountTotal();
        page.setPageCountTotal(pageCountTotal);
        //设置总页码
        int pageTotal = pageCountTotal / pageSize;
        if (pageCountTotal % pageSize > 0) {
            pageTotal++;
        }
        page.setPageTotal(pageTotal);
        //计算起始条数，查询内容
        int begin = (pageNo - 1) * pageSize;
        List<BlogTextTwo> blogList = blogMapper.queryBlogBeginAndPageSize(begin, pageSize);
        //设置每页内容
        page.setItems(blogList);
        return page;
    }

    //根据id删除用户id
    public void deleteBlogById(int id) {
        blogMapper.deleteBlogById(id);
    }

    /**
     * 个人博客列表
     *
     * @param userId
     * @param pageNo
     * @param pageSize
     * @return
     */
    public Page<BlogTextTwo> queryPersonalBlogByUserIdAndPageNoAndPageSize(int userId, int pageNo, int pageSize) {
        Page<BlogTextTwo> page = new Page<BlogTextTwo>();
        //设置当前页码
        page.setPageNo(pageNo);
        //设置每页个数
        page.setPageSize(pageSize);
        //设置总记录数
        int pageCountTotal = blogMapper.queryPersonalBlogCountTotalByUseId(userId);
        page.setPageCountTotal(pageCountTotal);
        //设置总页码
        int pageTotal = pageCountTotal / pageSize;
        if (pageCountTotal % pageSize > 0) {
            pageTotal++;
        }
        page.setPageTotal(pageTotal);
        int begin = (pageNo - 1) * pageSize;
        //计算起始条数，查询内容
        List<BlogTextTwo> blogList = blogMapper.queryPersonalBlogByUseIdAndBeginAndPageSize(userId, begin, pageSize);
        //设置每页内容
        page.setItems(blogList);
        return page;
    }

    /**
     * 通过模糊标题查询列表
     *
     * @param title
     * @param pageNo
     * @param pageSize
     * @return
     */
    public Page<BlogTextTwo> queryBlogByTitleAndPageNoAndPageSize(String title, int pageNo, int pageSize) {
        Page<BlogTextTwo> page = new Page<BlogTextTwo>();
        //设置当前页码
        page.setPageNo(pageNo);
        //设置每页个数
        page.setPageSize(pageSize);
        //设置总记录数
        int pageCountTotal = blogMapper.queryBlogCountTotalByTitle(title);
        page.setPageCountTotal(pageCountTotal);
        //设置总页码
        int pageTotal = pageCountTotal / pageSize;
        if (pageCountTotal % pageSize > 0) {
            pageTotal++;
        }
        page.setPageTotal(pageTotal);
        //计算起始条数，查询内容
        int begin = (pageNo - 1) * pageSize;
        List<BlogTextTwo> blogList = blogMapper.queryBlogByTitleAndBeginAndPageSize(title, begin, pageSize);
        ;
        //设置每页内容
        page.setItems(blogList);
        return page;
    }

    /**
     * 后台分模块管理blog
     *
     * @param type
     * @param pageNo
     * @param pageSize
     * @return
     */
    public Page<BlogTextTwo> queryBlogTextTwoByTypeAndBeginAndPageSize(int type, int pageNo, int pageSize) {
        Page<BlogTextTwo> page = new Page<BlogTextTwo>();
        //设置当前页码
        page.setPageNo(pageNo);
        //设置每页个数
        page.setPageSize(pageSize);
        //设置总记录数
        int pageCountTotal = blogMapper.queryBlogCountTotalByType(type);
        page.setPageCountTotal(pageCountTotal);
        //设置总页码
        int pageTotal = pageCountTotal / pageSize;
        if (pageCountTotal % pageSize > 0) {
            pageTotal++;
        }
        page.setPageTotal(pageTotal);
        //计算起始条数，查询内容
        int begin = (pageNo - 1) * pageSize;
        List<BlogTextTwo> blogList = blogMapper.queryBlogTextTwoByTypeAndBeginAndPageSize(type, begin, pageSize);
        //设置每页内容
        page.setItems(blogList);
        return page;
    }

    //根据UserId和BlogId查询此用户是否对该文章有点赞
    public Praise queryPraiseByBlogIdAndUserId(int blogId, int userId) {
        return blogMapper.queryPraiseByBlogIdAndUserId(blogId, userId);
    }

    //增加文章
    public void addPraise(Praise praise) {
        blogMapper.addPraise(praise);
    }

    //删除点赞记录
    public void deletePraise(int blogId, int userId) {
        blogMapper.deletePraise(blogId, userId);
    }

    //修改blog
    public void updateBlogById(Blog blog) {
        blogMapper.updateBlogById(blog);
    }

    //根据点赞数倒叙、起始条数、每页个数查询文章
    public Page<BlogText> queryBlogByPraiseAndPageNoAndPageSize(int pageNo, int pageSize) {
        Page<BlogText> page = new Page<BlogText>();
        //设置当前页码
        page.setPageNo(pageNo);
        //设置每页个数
        page.setPageSize(pageSize);
        //设置总记录数
        int pageCountTotal = blogMapper.queryBlogCountTotal();
        page.setPageCountTotal(pageCountTotal);
        //设置总页码
        int pageTotal = pageCountTotal / pageSize;
        if (pageCountTotal % pageSize > 0) {
            pageTotal++;
        }
        page.setPageTotal(pageTotal);
        //计算起始条数，查询内容
        int begin = (pageNo - 1) * pageSize;
        List<BlogText> blogList = blogMapper.queryBlogByPraiseAndBeginAndPageSize(begin, pageSize);
        //设置每页内容
        page.setItems(blogList);
        return page;
    }

    /**
     * 主管理员首页查询blog总数
     *
     * @return
     */
    public int queryBlogCountTotal() {
        return blogMapper.queryBlogCountTotal();
    }

    /**
     * 搜索个人博客页
     *
     * @param userId
     * @param pageNo
     * @param pageSize
     * @param searchValue
     * @return
     */
    public Page<BlogTextTwo> searchPersonalBlogByUserIdAndPageNoAndPageSizeAndSearchValue(int userId, int pageNo, int pageSize, String searchValue) {
        Page<BlogTextTwo> page = new Page<BlogTextTwo>();
        //设置当前页码
        page.setPageNo(pageNo);
        //设置每页个数
        page.setPageSize(pageSize);
        //设置总记录数
        int pageCountTotal = blogMapper.searchPersonalBlogCountTotalByUseIdAndSearchValue(userId, searchValue);
        page.setPageCountTotal(pageCountTotal);
        //设置总页码
        int pageTotal = pageCountTotal / pageSize;
        if (pageCountTotal % pageSize > 0) {
            pageTotal++;
        }
        page.setPageTotal(pageTotal);
        int begin = (pageNo - 1) * pageSize;
        //计算起始条数，查询内容
        List<BlogTextTwo> blogList = blogMapper.searchPersonalBlogByUseIdAndBeginAndPageSizeAndSearchValue(userId, begin, pageSize, searchValue);
        //设置每页内容
        page.setItems(blogList);
        return page;
    }

    /**
     * 根据类型查询文章总数
     *
     * @param type
     * @return
     */
    public int queryBlogCountTotalByType(int type) {
        return blogMapper.queryBlogCountTotalByType(type);
    }
}
